<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item :label="$t('employing_company')" prop="employerId">
        <employerSelect
          v-model="queryParams.employerId"
          @change="employerChange"
        ></employerSelect>
      </el-form-item>
      <el-form-item :label="$t('employee_name')" prop="employeeId">
        <el-select
          v-model="queryParams.employeeId"
          filterable
          :placeholder="$t('please_enter', { name: $t('employee_name') })"
        >
          <el-option
            v-for="(item, index) of employeeList"
            :label="item.employeeName"
            :value="item.id"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('year_month')" prop="month">
        <el-date-picker
          v-model="queryParams.month"
          type="month"
          :placeholder="$t('please_select', { name: $t('year_month') })"
          value-format="yyyy-MM"
          size="small"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{$t('search')}}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >{{$t('reset')}}</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['checkWork:checkWorkCount:export']"
          >{{$t('export')}}</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="attendanceList"
      @selection-change="handleSelectionChange"
      border
      :max-height="tableHeight"
    >
      <el-table-column
        :label="$t('employee')"
        align="center"
        prop="employeeName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
      :label="$t('rank')"
        align="center"
        prop="employerDeptName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
      :label="$t('employing_company')"
        align="center"
        prop="employerName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
      :label="$t('year_month')"
        align="center"
        prop="month"
        :show-overflow-tooltip="true"
      />
      <el-table-column
      :label="$t('number_of_late_arrivals')"
        align="center"
        prop="late"
        :show-overflow-tooltip="true"
      />
      <el-table-column
      :label="$t('number_of_early_leavings')"
        align="center"
        prop="leaveEarly"
        :show-overflow-tooltip="true"
      />
      <el-table-column
      :label="$t('number_of_missed_clock_ins')"
        align="center"
        prop="noPunchNum"
        :show-overflow-tooltip="true"
      />
      <el-table-column
      :label="$t('absenteeism')"
        align="center"
        prop="absenteeism"
        :show-overflow-tooltip="true"
      />
      <el-table-column
      :label="$t('overtime_hours')"
        align="center"
        prop="overTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column
      :label="$t('number_of_leave_days')"
        align="center"
        prop="leaveDays"
        :show-overflow-tooltip="true"
      />
      <el-table-column
      :label="$t('number_of_normal_attendance_days')"
        align="center"
        prop="normalDays"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['checkWork:checkWorkCount:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10,20,30,50,100, 200]"
    />
  </div>
</template>

<script>
import { listAttendance, delAttendance } from "@/api/checkWork/checkWorkCount";
import { pageListByEmployer } from "@/api/custom/employeeList";
import employerSelect from "@/views/components/custom/employerSelect.vue";

export default {
  name: "Attendance",
  components: {
    employerSelect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤统计表格数据
      attendanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        month: null,
        employeeId: null,
        employerId: null,
      },
      // 表单参数
      form: {},
      employeeList: [],
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    employerChange() {
      this.queryParams.employeeId = null;
      this.pageListByEmployer();
    },
    pageListByEmployer() {
      pageListByEmployer({
        employerId: this.queryParams.employerId,
      }).then((res) => {
        this.employeeList = res.data.records;
      });
    },
    /** 查询考勤统计列表 */
    getList() {
      this.loading = true;
      listAttendance(this.queryParams).then((response) => {
        this.attendanceList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(this.$t('delete_data'))
        .then(function () {
          return delAttendance(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t('operation_successful'));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/business/attendanceStatistics/export",
        {
          ...this.queryParams,
        },
        `${this.$t('attendance_statistics')}_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
